package com.example.modifyphototime

import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

class PhotoAdapter(private val photos: List<PhotoItem>) : RecyclerView.Adapter<PhotoAdapter.PhotoViewHolder>() {

    private var clickInterface: ClickInterface? = null

    fun setClickInterface(clickInterface : ClickInterface?){
        this.clickInterface = clickInterface
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotoViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_photo, parent, false)
        return PhotoViewHolder(view)
    }

    override fun onBindViewHolder(holder: PhotoViewHolder, position: Int) {
        val photo = photos[position]
        Glide.with(holder.itemView.context)
            .load(photo.uri)
            .into(holder.imageView)
        holder.textViewDate.text = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(
            Date(photo.timestamp)
        )
        holder.textViewDate.setOnClickListener {
            clickInterface?.onDateClick(photo.uri,photo.timestamp)
        }
    }

    override fun getItemCount(): Int = photos.size

    class PhotoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageView: ImageView = itemView.findViewById(R.id.imageView)
        val textViewDate: TextView = itemView.findViewById(R.id.textViewDate)
    }

    interface ClickInterface{
        fun onDateClick(uri: Uri,time:Long)
    }

}
